package OnJava8.functional;

/**
 * @Auther DRevelation
 * @Date 2020-05-20 下午 13:13
 * @Desc
 */
public class Strategize {
    Strategy strategy;
    String msg;

    Strategize(String msg) {
        strategy = new Soft();
        this.msg = msg;
    }

    void communiacte() {
        System.out.println(strategy.approach(msg));
    }

    void changeStrategy(Strategy strategy) {
        this.strategy = strategy;
    }

    public static void main(String[] args) {
        Strategy[] strategies = {
            new Strategy() {
                @Override
                public String approach(String msg) {
                    return msg.toUpperCase() + "?";
                }
            },
            msg -> msg.substring(0, 5),
            Unrelated::twice
        };
        Strategize s = new Strategize("Hello there");
        s.communiacte();
        for (Strategy newStrategy : strategies) {
            s.changeStrategy(newStrategy);
            s.communiacte();
        }
    }
}

interface Strategy {
    String approach(String msg);
}

class Soft implements Strategy {
    @Override
    public String approach(String msg) {
        return msg.toUpperCase() + "?";
    }
}

class Unrelated {
    static String twice(String msg) {
        return msg + " " + msg;
    }
}